﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Report : System.Web.UI.Page
{
    public int AssessmentID
    {
        get
        {
            int ID = -1;
            if (Request.QueryString["Aid"] != null)
                Int32.TryParse(Request.QueryString["Aid"], out ID);
            return ID;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/App_Data/Assessment.rdlc");
        ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("AssessmentData_AssessmentInfo", GenerateTitle()));
        ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("AssessmentData_AssessmentItemInfo", GenerateItems()));
        ReportViewer1.DataBind();
    }

    List<AssessmentReport> GenerateTitle()
    {
        List<AssessmentReport> ar = new List<AssessmentReport>();
        ar.Add(new AssessmentReport());
        LMSData.Assessment asmt = new LMSData.Assessment(AssessmentID);
        ar[0].AssessmentName = asmt.AssessmentName;
        ar[0].CourseName = asmt.Course.CourseName;
        return ar;
    }

    List<AssessmentItemReport> GenerateItems()
    {
        List<AssessmentItemReport> lair = new List<AssessmentItemReport>();
        LMSData.AssessmentItemController aic = new LMSData.AssessmentItemController();
        LMSData.AssessmentResponseController arc = new LMSData.AssessmentResponseController();
        LMSData.AssessmentItemCollection coll = aic.FetchByAid(AssessmentID);

        foreach (LMSData.AssessmentItem ai in coll)
        {
            LMSData.AssessmentResponseCollection arcl = arc.Fetch(ai.AssessmentItemID);
            foreach (LMSData.AssessmentResponse ar in arcl)
            {
                AssessmentItemReport air = new AssessmentItemReport();
                air.ItemID = ai.AssessmentItemID;
                air.ItemText = ai.ItemText;
                air.Response = ar.Response;
                air.Score = (int)ar.Score;
                air.UserAssessmentID = ar.UserAssessmentID;
                air.Count = arcl.Count;
                lair.Add(air);
            }
        }
        return lair;
    }

    private class AssessmentReport
    {
        public string AssessmentName { get; set; }
        public string CourseName { get; set; }
    }

    private class AssessmentItemReport
    {
        public int ItemID { get; set; }
        public string ItemText { get; set; }
        public int UserAssessmentID { get; set; }
        public string Response { get; set; }
        public int Score { get; set; }
        public int Count { get; set; }
    }
}
